package com.fancon.android.emoji.keyboard;

import java.util.ArrayList;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.fancon.android.emoji.Emoji;
import com.fancon.android.emoji.R;
/**
 * Adapter for pageview
 * @author binhbt
 *
 */
public class EmojiAdapter extends PagerAdapter{
	private Context mContext;
	private ArrayList<Emoji> listEmoji;
	private int size =0;
	public EmojiAdapter(Context context, ArrayList<Emoji> listEmoji){
		super();
		mContext = context;
		this.listEmoji = listEmoji;
	}
	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		// TODO Auto-generated method stub
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		size = listEmoji.size()%28==0?listEmoji.size()/28:listEmoji.size()/28+1;
		return size;
	}

	@Override
	public Object instantiateItem(View collection, int arg1) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) mContext
		.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.emoji_page_item, null);
		GridView grid = (GridView) view.findViewById(R.id.gridview);
		
		ArrayList<Emoji> sub = null;
		if(arg1 < size-1){
			sub = subist(listEmoji, arg1*28, (arg1+1)*28);
		}else if(arg1 == size-1){
			sub = subist(listEmoji, arg1*28, listEmoji.size());
		}
		EmojiGridAdapter adapter = new EmojiGridAdapter(mContext, sub);
		grid.setAdapter(adapter);
		((ViewPager) collection).addView(view, 0);
		return view;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == ((View) arg1);
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public int getItemPosition(Object object) {
	    return POSITION_NONE;
	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public ArrayList<Emoji> getListEmoji() {
		return listEmoji;
	}
	/**
	 * Dynamic change data for pageview in run time
	 * @param listEmoji
	 */
	public void setListEmoji(ArrayList<Emoji> listEmoji) {
		this.listEmoji.clear();
		this.listEmoji = listEmoji;
		this.notifyDataSetChanged();
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	/**
	 * Extract sub list from parent list by from and to index
	 * @param list
	 * @param from
	 * @param to
	 * @return
	 */
	public static ArrayList<Emoji> subist(ArrayList<Emoji> list, int from, int to){
		Object[] objArray =  list.toArray();
		ArrayList<Emoji> ret = new ArrayList<Emoji>();
		for(int i= from; i < to; i++){
			ret.add((Emoji) objArray[i]);
		}
		return ret;
		
	}
	/**
	 * Adapter for grid view
	 * @author binhbt
	 *
	 */
	class EmojiGridAdapter extends BaseAdapter {
	    //private Context mContext;
	    private ArrayList<Emoji> emojis;
	    public EmojiGridAdapter(Context c, ArrayList<Emoji> emojis) {
	        //mContext = c;
	        this.emojis = emojis;
	    }

	    public int getCount() {
	        return emojis.size();
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
	    	Emoji emoji;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	        	emoji = emojis.get(position);
	        } else {
	        	emoji = (Emoji) convertView;
	        }
	        emoji.setHeight(emoji.getWidth());
	        return emoji;
		}


	} 
}
